# -*- coding: utf-8 -*-
#   菜单和工具栏设计维护
# @Time    ： 2024/4/7  17:01
# @Auther  ： 徐良
# File     :  menuactionPanel.py

from PySide6.QtCore import Qt
from baseClass.baseQWidget import BaseQWidget
from baseUI.menuactionUI import Ui_Form
from share.delegateFunction import getConfigDelegate
from share.modelFunction import getTableModelBySql
from utils.pcWork import PCWork
import share.globalvar as gol
from share.dataFactory import DataFactory
from share.makeTree import MakeTree


class MenuactionPanel(BaseQWidget, PCWork, Ui_Form):

    def __init__(self, msDict, parent=None):
        super(MenuactionPanel, self).__init__()
        self.queryPanel = None
        self.setAttribute(Qt.WA_StyledBackground, True)
        self.setupUi(self)
        self.toolbarFlag = 'browse'
        self.stackedW = "Yes"

        self.baseDbType = gol.getValue('baseDbType')
        # print(self.baseDbType)
        msDict = eval(msDict)
        print(msDict)
        self.doInitWork(msDict)
        self.initPara()
        self.buildTree()
        self.readData(self.FSql)
        self.saveActionSignal.connect(self.setToolbarSaveButton)

        self.treeWidget.itemDoubleClicked.connect(self.onTreeItemDBClick)

    def initPara(self):
        """
        参数初始化
        :return:
        """
        # 取数据表名
        self.FTableName = self.getB1_FTableName()
        # sql = "select * from {tn} order by FNumber".format(tn=table)
        self.FSql = self.getB1_FSql()
        self.FTreeSql = self.getB1_FTreeSql()
        self.lbl_tableName.setText(self.getB1_FLabelTitle())  # 设置标签
        self.FFieldList = self.getB2_FFieldList()
        self.FFieldTypeList = self.getB2_FFieldTypeList()
        self.FJoinSqlList = self.getB2_FJoinSqlList()
        self.FFieldNameList = self.getB2_FFieldNameList()
        self.FAlignList = self.getB2_FAlignList()
        self.FWidthList = self.getB2_FTableWidthList()
        self.FVisibleList = self.getB2_FVisibleList()
        self.FWidgetList = self.getB2_FWidgetList()
        self.FIsEditList = self.getB2_FIsEditList()

    def buildTree(self):
        """
        建树
        :return:
        """
        # 建树
        makeTree = MakeTree(self.FTreeSql, self.treeWidget, self.baseDbType)
        makeTree.createTree()
        self.treeWidget.setColumnWidth(0, 150)

    def readData(self, sSql):
        """
        读取表数据
        :return:
        """
        pdic = {'sql': sSql, 'baseDbType': self.baseDbType,
                'fieldNameList': self.FFieldNameList, 'alignList': self.FAlignList}

        self.model = getTableModelBySql(pdic)
        self.tableView.setModel(self.model)
        self.tableView.setColumnWidthForList(self.FWidthList)
        self.tableView.setHideColumnForCol(self.FVisibleList)

        pdic = {'selOption': 0, 'isEditList': self.FIsEditList, 'widgetList': self.FWidgetList}
        delegate = getConfigDelegate(pdic)
        self.tableView.setItemDelegate(delegate)
        self.model.dataChangedSignal.connect(self.onModelDataChanged)

    def actionSelect(self, q):
        sel = q.text()
        if sel == '增加':
            self.tableAdd()
        elif sel == '插入':
            self.tableInsert()
        elif sel == '删除':
            self.tableDel()
        elif sel == '刷新':
            print('刷新')

    def save(self):
        """
        保存单据
        """
        self.tableSave()


if __name__ == '__main__':
    import sys
    from PySide6.QtWidgets import QApplication
    app = QApplication(sys.argv)
    dic = "{'msFNumber': '69.02', 'msClass': '10', 'msSaveType':1}"
    window = MenuactionPanel(dic)
    window.show()
    sys.exit(app.exec())


